﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace cds_betadev01.source
{
    enum State { IsMovingLeft,IsMovingRight,IsNotMoving }
    class Player:ObjDisplay
    {
        
        private float MoveSpeed = 20f;
        private Keys left = Keys.Left;
        private Keys right = Keys.Right;
        private float maxMove = 3000.0f;
        private float Pisteposition = 0.0f; //position lateral : 0 -> millieu |Maxmove -> bord droit|-MaxMove -> bord gauche
        private Matrix Global;
        private State statement = State.IsNotMoving;
        private Matrix leftMoveMatrix = Matrix.CreateRotationZ(MathHelper.ToRadians(30.0f));
        private Matrix rightMoveMatrix = Matrix.CreateRotationZ(-MathHelper.ToRadians(30.0f));
        

        
        public Keys Left{get;set;}
        private Keys Right{get;set;}
        public Player()
        {
            
        }
        public Player(Vector3 position, float scale, Vector3 rot,Model mmodel)
        {
            base.init(position,scale,rot,mmodel);
            Global = World;
        }
        public void upload(int interval,KeyboardState kb)
        {

            bool l,r;
            l = kb.IsKeyDown(left);
            r = kb.IsKeyDown(right);
            if(l && !r)
            {
                moveLeft(interval);
            }
            else if (r && !l)
            {
                moveRight(interval);
            }
            else
            {
                UploadPosition(Matrix.Identity);
            }
        }
        private void moveLeft(int interval)
        {
            if (statement != State.IsMovingLeft)
            {
                statement = State.IsMovingLeft;
            }
            
             Pisteposition -= interval * MoveSpeed;
             checkRoadColision();
             UploadPosition(leftMoveMatrix);
        }
        private void moveRight(int interval)
        {
            if (statement != State.IsMovingRight)
            {
                statement = State.IsMovingRight;
            }

            Pisteposition += interval * MoveSpeed;
            checkRoadColision();
            UploadPosition(rightMoveMatrix);
        }
        private void checkRoadColision()
        {
            if (Pisteposition >= maxMove)
            {
                Pisteposition = maxMove;
            }
            if (Pisteposition <= -maxMove)
            {
                Pisteposition = -maxMove;
            }
        }
        private void UploadPosition(Matrix mState)
        {
            World = mState *Matrix.CreateTranslation(Vector3.Right* Pisteposition)*Global;
        }
         private Matrix getMatrixState()
        {
            switch (statement)
            {
                case State.IsMovingLeft:
                    return leftMoveMatrix;
                    
                case State.IsMovingRight:
                    return rightMoveMatrix;
                    
                default :
                    return Matrix.Identity;
                    
            }
            }
        }
    }


